<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addClassify">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区 -->
      <tree-table
        class="table-cat"
        :data="cateList"
        :columns="columns"
        show-index
        index-text="#"
        border
        :expand-type="false"
        :selection-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <span
            :style="styleColor(scope.row.cat_deleted)"
            :class="scope.row.cat_deleted===false?'el-icon-success':'el-icon-error'"
          ></span>
        </template>
        <!-- 排序 -->
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1" size="mini" type="success">二级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===2" size="mini" type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operation">
          <!-- 编辑 -->
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <!-- 删除 -->
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 弹出层添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addClassifyVisible"
      width="50%"
      @close="addClassifyClosed"
    >
      <el-form
        :model="addClassForm"
        :rules="classRules"
        ref="ruleForm"
        class="demo-ruleForm"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addClassForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_pid">
          <div class="block">
            <span class="demonstration"></span>
            <el-cascader
              change-on-select
              clearable
              style="width:100%"
              v-model="selectKeys"
              :options="parentCateList"
              :props="cascaderProps"
              @change="selectedChange"
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addClassifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitClassify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { truncate } from "fs";
export default {
  data() {
    return {
      classRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 3, max: 7, message: "长度在 3 到 7 个字符", trigger: "blur" }
        ]
      },
      parentCateList: [],
      addClassForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      addClassifyVisible: false,
      cateList: [],
      pageTotal: null,
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
          headerAlign: "center"
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isok",
          headerAlign: "center"
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "sort",
          headerAlign: "center"
        },
        {
          label: "操作",
          prop: "cat_deleted",
          type: "template",
          template: "operation",
          headerAlign: "center"
        }
      ],
      // 级联选择器的具体属性
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        expandTrigger: "hover"
      },
      selectKeys: []
    };
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getCateData();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getCateData();
    },
    async getCateData() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.cateList = res.data.result;
      this.pageTotal = res.data.total;
    },
    styleColor(val) {
      return !val ? "color:lightgreen" : "color:red";
    },
    addClassify() {
      this.getParentCateList();
      this.addClassifyVisible = true;
    },
    async getParentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: {
          type: 2
        }
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.parentCateList = res.data;
    },
    selectedChange(v) {
      if (v.length > 0) {
        this.addClassForm.cat_pid = v[v.length - 1];
        this.addClassForm.cat_level = v.length;
      }
    },
    submitClassify() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return;
        this.addClassifyVisible = false;
        const { data: res } = await this.$http.post(
          "categories",
          this.addClassForm
        );
        console.log(res);
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
      });
    },
    addClassifyClosed() {
      this.$refs.ruleForm.resetFields();
      this.addClassifyVisible = false;
      this.selectKeys = [];
      this.addClassForm.cat_pid = 0;
      this.addClassForm.cat_level = 0;
    }
  },
  created() {
    this.getCateData();
  }
};
</script>


<style lang="less" scoped>
</style>
